package de.tabuma.shopping.entity;

import java.util.*;
import javax.jdo.annotations.*;
import de.tabuma.shopping.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/07/21 19:37:33 $
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Order implements Cloneable {

    public Order() {
        this.canceled = false;
        this.payed = false;
        this.orderDate = GregorianCalendar.getInstance().getTime();
        this.token = createToken();
    }

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    public Long getId() {
        return id;
    }

    public int getState() {
        if(canceled && payed) {
            return constants.STATE_PAYED_CANCELED;
        }
        if(payed) {
            return constants.STATE_PAYED;
        }
        if(canceled) {
            return constants.STATE_CANCELED;
        }
        return constants.STATE_OPEN;
    }

    @Persistent
    Boolean payed;
    public boolean getPayed() {
        return payed;
    }
    public void setPayed(Boolean payed) {
        this.payed = payed;
    }

    @Persistent
    Date payDate;
    public Date getPayDate() {
        return payDate;
    }
    public void setPayDate(Date payDate) {
        this.payDate = payDate;
    }

    public void pay() {
        this.payed = true;
        this.payDate = GregorianCalendar.getInstance().getTime();
    }



    @Persistent
    Boolean canceled;
    public boolean getCanceled() {
        return canceled;
    }
    public void setCanceled(Boolean canceled) {
        this.canceled = canceled;
    }

    @Persistent
    Date cancelDate;

    public Date getCancelDate() {
        return cancelDate;
    }

    public void setCancelDate(Date cancelDate) {
        this.cancelDate = cancelDate;
    }

    public void chancel() {
        this.canceled = true;
        this.cancelDate = GregorianCalendar.getInstance().getTime();
    }


    @Persistent
    Date orderDate;

    public Date getOrderDate() {
        return orderDate;
    }
    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    @Persistent
    String token;
    public String getToken() {
        return token;
    }
    public void setToken(String token) {
        this.token = token;
    }

    public static String createToken() {
        int a = 30+(int)(Math.random()*70);
        String ergebnis = "";
        for(int i=0;i<a;i++) {
            int b = (int) (Math.random()*constants.tokenChars.length);
            char c = constants.tokenChars[b];
            ergebnis += c;
        }
        return ergebnis;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Order myOrder = new Order();
        myOrder.setCancelDate(cancelDate);
        myOrder.setCanceled(canceled);
        myOrder.setOrderDate(orderDate);
        myOrder.setPayDate(payDate);
        myOrder.setPayed(payed);
        myOrder.setToken(token);
        return myOrder;
    }



}
